import re
import os
import pylab
file = open('new.txt','r')
lines = file.readlines()
file.close()

PM = 'PM2\.5; '
Ozone = 'Ozone; '
date = '(?P<date>\d+-\d+-\d+); '
time = '(?P<time>\d+:\d+); '
pm_low = '(?P<pm_low>\d+\.\d+); '
pm_high = '(?P<pm_high>\d+(.\d+)*); '
ozone_low = '(?P<ozone_low>(-)*\d+\.\d+); '
ozone_high = '(?P<ozone_high>\d+(.\d+)*); '
word = '(\w+\s+)*\w+\s*' 
double = '//\s*'

average_pm = 'Past 24hr: PM2.5 avg; '
average_ozone = 'Ozone 8hr high; '


r0 = 'BeijingAir: '+date+time+PM        +pm_low+pm_high+word+double+Ozone        +ozone_low+ozone_high
r1 = 'BeijingAir: '+date+time+average_pm+pm_low+pm_high+word+double+average_ozone+ozone_low+ozone_high
r2 = 'no data'

data_pm_low = []
data_pm_high = []
data_ozone_low = []
data_ozone_high = []

for l in lines:
    o = re.search(r0, l)
    if not o:
        o = re.search(r1, l)
    if o:
        #print 'date %s, time %s, pm low %s, pm high %s, ozone low %s, ozone high %s'%(o.group('date'), o.group('time'), o.group('pm_low'), o.group('pm_high'),o.group('ozone_low'),o.group('ozone_high'))    
        #get data here
        data_pm_low.append(float(o.group('pm_low')))
        data_pm_high.append(float(o.group('pm_high')))
        data_ozone_low.append(float(o.group('ozone_low')))
        data_ozone_high.append(float(o.group('ozone_high')))

    if not o:
        o = re.search(r2, l)
    if not o:
        print 'Error string ', l
        os._exit(3)

print len(data_pm_low)
pylab.fill_between(
    range(len(data_pm_low)), data_pm_low, data_pm_high

)
#    range(len(data_pm_high)), data_pm_high, fmt='b-', xdate=True,ydate=False,
#    range(len(data_ozone_low)), data_ozone_low, fmt='y-', xdate=True,ydate=False,
#    range(len(data_ozone_high)), data_ozone_high, fmt='r-', xdate=True,ydate=False
pylab.show()
